package userServlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import userDao.UserDao;
import userEntity.User;
import userImplment.UserImplement;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//从前端获得用户输入的用户名和密码
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		String Uname = request.getParameter("Uname");
		String passWord = request.getParameter("passWord");
		//多态新建用户实现类对象
		UserDao userImplement=new UserImplement();
		HttpSession session = request.getSession();
		if(userImplement.Login(Uname, passWord)) {
			//如果用户名和密码正确，则将个人信息保存到session，并跳转到个人中心主页
			User user=userImplement.Query(Uname);
			session.setAttribute("user", user);
			out.write("true");
			//request.getRequestDispatcher("my.jsp").forward(request, response);
		}else if(Uname.equals("admin")&&passWord.equals("admin")) {
			//如果是管理员登录，则跳转到管理员首页
			out.write("admin");
			//request.getRequestDispatcher("admin.jsp").forward(request, response);
		}else {
			//如果用户名密码错误，则跳转的错误提示页面
			out.write("false");
			//request.getRequestDispatcher("myLoginOrror.jsp").forward(request, response);
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
